package com.lovo.Atm.frame;

import com.lovo.Atm.pojo.UserDO;
import com.lovo.Atm.service.UserService;
import com.lovo.Atm.service.impl.UserServiceImpl;
import com.lovo.Atm.util.JDUtil;
import com.lovo.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Save extends JPanel implements ActionListener {
    private JButton backImg;
    private JButton saveImg;
    private JLabel note;
    private JTextField printSave;
    private Home frame;
    private Icon saveBtn = Util.iconImg("/images/buttonSave.jpg");
    private Icon backBtn = Util.iconImg("/images/buttonOff.jpg");
    private UserService userService = new UserServiceImpl();

    public Save(Home frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);


        //返回按钮，菜单界面
        backImg = new JButton(backBtn);
        backImg.setBounds(870, 300, 140, 50);
        this.add(backImg);
        backImg.addActionListener(this);

        //存款按钮
        saveImg = new JButton(saveBtn);
        saveImg.setBounds(0, 300, 140, 50);
        this.add(saveImg);
        saveImg.addActionListener(this);

        //存款文字提示
        note = new JLabel("请输入存款金额：");
        note.setBounds(300, 230, 300, 30);
        note.setFont(new Font("宋体", Font.BOLD, 20));
        this.add(note);

        //存款文本框
        printSave = new JTextField();
        printSave.setBounds(600, 230, 100, 30);
        printSave.setFont(new Font("宋体", Font.BOLD, 20));
        this.add(printSave);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == backImg) {
            frame.switchPanel("menu");
        }
        if (e.getSource() == saveImg) {
            JDUtil jd = new JDUtil();
            UserDO loginUser = jd.getById(frame.getLoginUser().getId());
            //存入的金额
            double saveMoney = Double.valueOf(printSave.getText());
            loginUser.setBlance(frame.getLoginUser().getBlance());
            userService.saveBlance(frame.getLoginUser().getId(),saveMoney);
            JOptionPane.showMessageDialog(this, "存款成功");
            frame.switchPanel("menu");
        }
    }

}
